package com.kh.pajx.sop.view.action.order;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.kh.pajx.sop.base.ModelDrivenBaseAction;
import com.kh.pajx.sop.domain.order.ReplaceCardOrder;
import com.kh.pajx.sop.domain.order.ReplaceCardOrderDetail;
import com.kh.pajx.sop.domain.order.ReplaceCardOrderPhoto;
import com.kh.pajx.sop.domain.order.ReplaceCardOrderRelation;
import com.kh.pajx.sop.util.common.DwzAjaxResult;
import com.opensymphony.xwork2.ActionContext;

/**
 * 补卡单据明细Struts2控制器
 * @author:	azzcsimp
 * @Createdate:	2014年11月12日 下午6:07:00
 */
@Controller
@Scope("prototype")
public class ReplaceCardOrderDetailAction extends ModelDrivenBaseAction<ReplaceCardOrderDetail> {

	private HttpServletRequest request;
	private static final long serialVersionUID = -4739631030621284811L;

	public String list() {
		try {
			String keyword = StringUtils.trim(ServletActionContext.getRequest().getParameter("keyword"));
			if (StringUtils.isNotEmpty(keyword)) {
				model.setStuName(keyword);
			}
			replaceCardOrderDetailService.list(model, pageNum, numPerPage);
			ActionContext.getContext().put("keyword", keyword);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "list";
	}

	/**
	 * 查看详情页面
	 * @author xlw
	 * @createDate 2014年12月10日 下午3:14:34 
	 * @param
	 * @return
	 */
	public String lookdetail() {
		DwzAjaxResult dwz = new DwzAjaxResult();
		request = ServletActionContext.getRequest();
		String rcoId = request.getParameter("rcoId");
		try {
			//根据订单编号查询订单
			ReplaceCardOrder replaceCardOrder = replaceCardOrderService.getById(rcoId);
			ActionContext.getContext().getValueStack().push(replaceCardOrder);
			//根据订单编号查询订单详情
			List<ReplaceCardOrderDetail> rcodDetails = replaceCardOrderDetailService.getByRcoId(rcoId);
			request.setAttribute("rcodDetail", rcodDetails);
			//根据订单编号查询照片路径
			List<ReplaceCardOrderPhoto> rcodPhotos = replaceCardOrderPhotoService.getByRcoId(rcoId);
			request.setAttribute("rcodPhoto", rcodPhotos);
			//根据订单号获取所有关联信息，进行流程跟踪显示
			List<ReplaceCardOrderRelation> rcodRelation = replaceCardOrderRelationService
					.getOrderRelationsByRcoId(rcoId);
			request.setAttribute("rcodRelation", rcodRelation);

		} catch (Exception e) {
			e.printStackTrace();
			dwz.setStatusCode("300");
			dwz.setMessage("补卡失败！：<Br>" + e.getMessage());
			DwzAjaxResult.josnMessage(dwz);
		}

		return "lookdetail";
	}

	private String fileName;
	private InputStream excelFile;

	/**
	 * 导出明细列表的学生信息
	 * @author xlw
	 * @createDate 2014年12月22日 上午10:00:46 
	 * @param
	 * @return
	 */
	public String exportOrderDetail() {
		request = ServletActionContext.getRequest();
		String rcoId = request.getParameter("orderDetailId");
		String rcoNo = request.getParameter("orderDetailNo");
		String sclNo = rcoNo.substring(0, 5);
		String path = ServletActionContext.getServletContext().getRealPath(
				"/tempFile/" + this.getCurrentUser().getUserId());
		fileName = "订单" + rcoNo + "详情.zip";
		try {
			fileName = new String(fileName.getBytes("UTF-8"), "8859_1");
			String zipFileName = replaceCardOrderDetailService.exportExcelOrderDetail(rcoId, sclNo, path,
					request.getRemoteAddr());
			File f = new File(zipFileName);
			if (f.exists()) {//进行判断返回路径下是否存在该文件
				excelFile = new FileInputStream(zipFileName);
			} else {
				return null;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "excel";
	}

	/**
	 * 进行临时文件删除
	 * @author xlw
	 * @createDate 2014年12月24日 上午11:01:23 
	 * @param
	 * @return
	 */
	public void delDir() {
		String path = ServletActionContext.getServletContext().getRealPath(
				"/tempFile/" + this.getCurrentUser().getUserId());
		File f = new File(path);
		System.out.println(path);
		boolean flag = deleteDir(f);

	}

	private boolean deleteDir(File dir) {
		if (dir.isDirectory()) {
			String[] children = dir.list();
			//递归删除目录中的子目录下
			for (int i = 0; i < children.length; i++) {
				boolean success = deleteDir(new File(dir, children[i]));
				if (!success) {
					return false;
				}
			}
		}
		// 目录此时为空，可以删除
		return dir.delete();
	}

	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

	public InputStream getExcelFile() {
		return excelFile;
	}

	public void setExcelFile(InputStream excelFile) {
		this.excelFile = excelFile;
	}

}
